package com.eqdd.yiqidian.ui.mine.expense

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.bumptech.glide.Glide
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.bean.ExpenseCheckProcessBean
import com.eqdd.yiqidian.bean.ExpenseInfoBean
import com.eqdd.yiqidian.http.HttpCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.ListHttpResult
import com.eqdd.yiqidian.kotlin.KotlinBaseActivity
import com.eqdd.yiqidian.utils.ToastUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_expense_info.*
import kotlinx.android.synthetic.main.list_item_124.view.*
import kotlinx.android.synthetic.main.list_item_125.view.*
import kotlinx.android.synthetic.main.list_item_126.view.*
import org.jetbrains.anko.textColor

/**
 * @author吕志豪 .
 * @date 18-3-23  下午1:18.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */
class MyExpenseInfoActivity : KotlinBaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_expense_info)
        super.onCreate(savedInstanceState)
        initTopBar("报销详情")
        val itemId = intent.getStringExtra("expenseId")
        initData(itemId)


    }

    private fun initData(itemId: String?) {

        OkGo.post<HttpResult<ExpenseInfoBean>>(HttpConfig.BASE_URL + HttpConfig.REIMBURSE_INFO_ID)
                .params("reimburseId", itemId)
                .params("userGuid", user?.guid)
                .execute(object : HttpCallBack<HttpResult<ExpenseInfoBean>>(this@MyExpenseInfoActivity) {

                    override fun onSuccess(response: Response<HttpResult<ExpenseInfoBean>>) {
                        val httpResult = response.body()
                        if (httpResult.status == 200) {
                            ToastUtil.showShort(httpResult.msg)
                            initView(httpResult.items)

                        }
                    }
                })
        OkGo.post<ListHttpResult<ExpenseCheckProcessBean>>(HttpConfig.BASE_URL + HttpConfig.REIMBURSE_PROCESS)
                .params("remiburseId", itemId)
                .execute(object : HttpCallBack<ListHttpResult<ExpenseCheckProcessBean>>(this@MyExpenseInfoActivity) {
                    override fun onSuccess(response: Response<ListHttpResult<ExpenseCheckProcessBean>>) {
                        val httpResult = response.body()
                        if (httpResult.status == 200) {
                            initProcess(httpResult.items)
                        }
                    }

                })
    }


    private fun initView(bean: ExpenseInfoBean) {
        tv_name.text = bean.remibursetitle
        tv_code.text = bean.code
        tv_reporter.text = bean.createName
        tv_checker.text = bean.checkerName
        tv_time.text = bean.createTime
        tv_all_money.text = "¥" + bean.totalMoney.toString()
        tv_all_money.textColor = Color.RED
        tv_remark.text = bean.remarks
        bean.detailList?.forEachIndexed { index, it ->
            val view = LayoutInflater.from(this).inflate(R.layout.list_item_125, null)
            view.tv_num.text = "报销条目" + (index + 1)
            view.tv_money.text = "¥" + it.reimburseMoney.toString()
            view.tv_subject.text = it.reimburseType
            if (it.isBudget == 1) {
                view.layout_amount.visibility = View.VISIBLE
                view.tv_budget_amount.text = "¥" + it.budgetMoney.toString()
                view.tv_budget_amount.textColor = Color.RED
                view.tv_budget_surplus.text = "¥" + it.remainderMoney.toString()
                view.tv_budget_surplus.textColor = Color.RED
            } else {
                view.layout_amount.visibility = View.GONE
            }

            view.tv_introduce.text = it.explain
            if (it.billImageList != null) {
                view.listView.visibility = View.VISIBLE
                it.billImageList?.forEach { imageView ->
                    val v = LayoutInflater.from(this).inflate(R.layout.list_item_124, null)
                    Glide.with(this)
                            .load(imageView)
                            .into(v.image)
                    view.listView.addView(v)
                }
                linearLayout.addView(view)
            } else {
                view.listView.visibility = View.GONE
            }

        }
    }

    private fun initProcess(items: List<ExpenseCheckProcessBean>?) {
        val inflater = LayoutInflater.from(this@MyExpenseInfoActivity)
        if (items.orEmpty().isNotEmpty()) {
            layout_all_progress.visibility = View.VISIBLE
        }

        items?.forEach {
            val view = inflater.inflate(R.layout.list_item_126, null)
            val status = if (it.status == 0) {
                "同意"
            } else {
                "驳回"
            }
            view.tv_process_name.text = it.staffName + "    于    " + it.createTime + "  " + status
            view.tv_process_reason.text = it.message
            layout_process.addView(view)
        }

    }


}